package com.slg.android.manyanime;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.adapter.ZhuifanOuterAdapter;
import com.slg.android.manyanime.bean.Animation;
import com.slg.android.manyanime.utils.ApiUtils;
import com.slg.android.manyanime.utils.HttpTool;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;


public class ZhuifanActivity extends FragmentActivity {

    @ViewInject(R.id.zhuifan_out_listview)
    private ListView zhuifanlist;
    @ViewInject(R.id.common_title_function_textView)
    private TextView title;

    private ZhuifanOuterAdapter adapter;

    private List<List<Animation>> data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_zhuifan);
        ViewUtils.inject(this);
        data = new ArrayList<>();


        parseData();

    }

    public void parseData() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, ApiUtils.ZHUIFAN_PAGES
                , new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                        String result = stringResponseInfo.result;
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONArray innerJsonArray = jsonArray.getJSONArray(i);
                                List<Animation> innerlist = new ArrayList<Animation>();
                                for (int j = 0; j < innerJsonArray.length(); j++) {
                                    Animation animation = new Animation();
                                    animation.parseJson(innerJsonArray.getJSONObject(j));
                                    innerlist.add(animation);
                                    Log.i("str",animation.getName());
                                }
                                data.add(innerlist);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter = new ZhuifanOuterAdapter(ZhuifanActivity.this, data);
                        zhuifanlist.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        Log.i("str",data.size()+" ");
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                    }
                }
        );


    }


}
